package com.hb.trade.api.routine.controller.project.opening;

import com.hb.core.pojo.project.opening.dto.BidOpeningScheduleDTO;
import com.hb.core.pojo.project.opening.dto.BidOpeningScheduleTemplateDTO;
import com.hb.core.pojo.project.opening.entity.BidOpeningSchedule;
import com.hb.core.pojo.project.opening.entity.BidOpeningScheduleTemplate;
import com.hb.core.pojo.project.section.dto.BidSectionDTO;
import com.hb.trade.client.project.BidOpeningClient;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.fzzn.core.auth.shiro.util.ShiroUtil;
import org.fzzn.core.model.enums.MsgEnum;
import org.fzzn.core.model.msg.Message;
import org.fzzn.core.model.msg.ResponseDto;
import org.fzzn.core.util.ResultUtil;
import org.fzzn.core.web.anno.Version;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 招标文件开标一览表控制接口
 *
 * @author zhaojk
 */
@Version(1.0)
@RestController
@RequestMapping("/{version}")
@Api(tags = "招标文件开标一览表控制接口")
public class ApiBidOpeningScheduleController {

    protected final BidOpeningClient bidOpeningClient;

    public ApiBidOpeningScheduleController(BidOpeningClient bidOpeningClient) {
        this.bidOpeningClient = bidOpeningClient;
    }

    //----------------------------------------- 开标一览表业务模块 ---------------------------------------------

    /**
     * 获取常用开标一览表信息
     * @author tian
     * @date 2022-2-27 11:16
     * @return org.fzzn.core.model.msg.ResponseDto
     */
    @ApiOperation(value = "查询开标一览表模板", notes = "查询开标一览表模板")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "projectType", value = "项目分类（1: 工程, 2: 货物, 3: 服务）", dataTypeClass = Integer.class, required = true)
    })
    @PostMapping("/bidOpeningSchedule/scheduleTemplates")
    public ResponseDto scheduleTemplates(@RequestBody BidOpeningScheduleTemplateDTO bidOpeningScheduleTemplateDTO) {
        Message<List<BidOpeningScheduleTemplate>> message = bidOpeningClient.scheduleTemplates(bidOpeningScheduleTemplateDTO);
        return ResultUtil.msg(message);
    }

    /**
     * 保存招标文件对应的开标项
     *
     * @param bidOpeningSchedule
     * @return
     */
    @ApiOperation(value = "保存招标文件的开标一览表", notes = "保存招标文件的开标一览表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "fileId", value = "招标文件Id", dataTypeClass = Long.class, required = true),
            @ApiImplicitParam(name = "bidOpeningSchedules", value = "开标一览表集合对象", dataTypeClass = List.class),
            @ApiImplicitParam(name = "bidOpeningSchedules[0].bidOpeningName", value = "开标项名称", dataTypeClass = String.class),
            @ApiImplicitParam(name = "bidOpeningSchedules[0].unitName", value = "单位名称", dataTypeClass = String.class),
            @ApiImplicitParam(name = "bidOpeningSchedules[0].fillType", value = "填写类型(1.文字 2 数字)", dataTypeClass = int.class),
            @ApiImplicitParam(name = "bidOpeningSchedules[0].sortNum", value = "排序号", dataTypeClass = int.class)
    })
    @PostMapping("/bidOpeningSchedule/saveSchedulesToBidFile")
    public ResponseDto saveSchedulesToBidFile(@RequestBody BidOpeningScheduleDTO bidOpeningSchedule) {
        Message<Boolean> message = bidOpeningClient.saveSchedulesToBidFile(bidOpeningSchedule);
        return ResultUtil.msg(message);
    }

    /**
     * 查询当前登录用户的常用开标项
     * 以企业ID为查询条件
     * 即同企业下不同用户看到的常用开标项是统一的
     *
     * @return
     * @deprecated 暂时不用
     */
    @Deprecated
    @ApiOperation(value = "查询常用开标项(当前企业的)", notes = "查询常用开标项(当前企业的)")
    @PostMapping("/bidOpeningSchedule/listSchedules")
    public ResponseDto listSchedules() {
        // 获取当前登录用户的企业ID
        Long orgId = ShiroUtil.getOrgId();
        Message<List<BidOpeningSchedule>> schedulesMsg = bidOpeningClient.listBidOpeningSchedulesByOrgId(orgId);
        return ResultUtil.msg(schedulesMsg);
    }

    /**
     * 查询已经保存的开标项列表
     * 指定fileId
     *
     * @return
     */
    @ApiOperation(value = "查询已经保存的开标项列表", notes = "查询已经保存的开标项列表")
    @PostMapping("/bidOpeningSchedule/listSchedulesByFileId")
    public ResponseDto listSchedulesByFileId(@RequestBody BidOpeningScheduleDTO bidOpeningSchedule) {
        Long fileId = bidOpeningSchedule.getFileId();
        Integer projectType = bidOpeningSchedule.getProjectType();
        if (fileId == null || projectType == null) {
            return ResultUtil.msg(MsgEnum.INCORRECT_PARAM);
        }
        Message<List<BidOpeningSchedule>> schedulesMsg = bidOpeningClient.listByFileId(fileId, projectType);
        return ResultUtil.msg(schedulesMsg);
    }

    @ApiOperation(value = "根据标段Id查询开标一览表模板", notes = "查询已经保存的开标项列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sectionId", value = "标段Id", dataTypeClass = Long.class, required = true)
    })
    @PostMapping("/bidOpeningSchedule/listBySectionId")
    public ResponseDto listBySectionId(@RequestBody BidSectionDTO bidSectionDTO) {
        Message<List<BidOpeningSchedule>> schedulesMsg = bidOpeningClient.listBySectionId(bidSectionDTO);
        return ResultUtil.msg(schedulesMsg);
    }
}
